package com.huawei.petapp2.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.huawei.petapp2.R;
import com.huawei.petapp2.adapter.InnerMessageAdapter;
import com.huawei.petapp2.constant.PushConst;
import com.huawei.petapp2.entity.InnerMessageBean;
import com.huawei.petapp2.utils.DateUtil;
import com.huawei.petapp2.utils.PushSPUtil;
import com.huawei.petapp2.utils.SPUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static android.view.View.VISIBLE;

public class InnerMessageCenterActivity extends AppCompatActivity {

    private RecyclerView lvInnerMessage;
    InnerMessageAdapter innerMessageAdapter;
    List<InnerMessageBean> messageList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inner_message_center);
        // 初始化View
        initView();
        initMessage();
        updateMessageList();
    }

    /**
     * 初始化View
     */
    private void initView() {
        // 顶部返回
        ImageView mIvBack = findViewById(R.id.title_back);
        // 顶部右侧按钮
        ImageView ivClear = findViewById(R.id.title_right);
        ivClear.setImageDrawable(getDrawable(R.drawable.ic_inner_message_clear));
        ivClear.setVisibility(VISIBLE);
        lvInnerMessage = findViewById(R.id.lvInnerMessage);

        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 返回
                finish();
            }
        });
        ivClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PushSPUtil.clearMessage(getApplicationContext());
                innerMessageAdapter.clear();
            }
        });
    }

    private void updateMessageList() {
        Map<String, String> data = PushSPUtil.readMessage(getApplicationContext());
        Context context = getApplicationContext();
        for (String key : data.keySet()) {
            InnerMessageBean innerMessage = new InnerMessageBean(context, key, data.get(key));
            messageList.add(innerMessage);
        }
        innerMessageAdapter = new InnerMessageAdapter(messageList);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        //设置布局管理器
        lvInnerMessage.setLayoutManager(manager);
        //设置为垂直布局，这也是默认的
        manager.setOrientation(RecyclerView.VERTICAL);
        lvInnerMessage.setAdapter(innerMessageAdapter);
    }

    private void initMessage() {
        //默认保存一条消息到消息中心
        boolean isSaved = (boolean) SPUtil.get(this, "is_saved_push", false);
        if (isSaved) {
            return;
        }
        PushSPUtil.saveMessage(this, "宠物商品上新啦"
                        + PushConst.PUSH_SPLIT + DateUtil.getCurrentFormatDate(),
                "最新猫粮上新，快去看看吧，啊实打实的看见爱上房间爱嘉实基金爱神的箭爱神的箭");
        SPUtil.put(this, "is_saved_push", true);
    }
}